// miniprogram/pages/signUpAsk/signUpAsk.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    signUpList: [],
    user: "",
    grade: "",
    group: "",
    tutor: "",
    contestant_id: "",
    state: "",
    // 导航栏
    navbar: ['信息核对区', '已报名的比赛'],
    currentTab: 0,
    // 比赛picker
    gameTypes: [],
    gameIndex: 0,
    gameType: "点击选择比赛",
  },

  // 导航栏
  navbarTap: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
  },

  // 比赛picker
  changeGamePicker: function (e) {
    this.setData({
      gameIndex: e.detail.value,
    })
    // console.log(this.data.sexType, this.data.sexIndex, this.data.sexTypes[1])
    // output：男 1 女
    // 分开，先更新index再更新到性别，否则可能出错如上
    this.setData({
      gameType: this.data.gameTypes[this.data.gameIndex],
      grade: this.data.signUpList[this.data.gameIndex].grade,
      group: this.data.signUpList[this.data.gameIndex].group,
      tutor: this.data.signUpList[this.data.gameIndex].tutor,
      contestant_id: this.data.signUpList[this.data.gameIndex].contestant_id,
      state: this.data.signUpList[this.data.gameIndex].state,
    })
  },


  // 点击显示基本信息
  // showBaseInfo:function() {
  //   let that = this
  //   wx.showModal({
  //     title: '提示',
  //     content: '姓名:' + that.data.user.name + "\r\n性别:" + that.data.user.sex,
  //     showCancel: false,
  //     success: function (res) {
  //       if (res.confirm) {
  //         console.log('用户点击确定')
  //       } else if (res.cancel) {
  //         console.log('用户点击取消')
  //       }
  //     }
  //   })  
  // },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    wx.cloud.callFunction({
      name: "getSignUpRecordByOpenId",
      data: {
        openId: options.openId
      },
      success: res => {
        console.log("调用云函数获取报名记录成功", res)
        that.setData({
          signUpList: res.result.data
        })
        if(that.data.signUpList.length > 0) {
          var tempGameTitleList = []
          for (var i = 0; i < that.data.signUpList.length; ++i) {
            tempGameTitleList.push(that.data.signUpList[i].gameTitle)
          }
          // console.log(tempGameTitleList)

          that.setData({
            user: that.data.signUpList[0].user,
            gameTypes: tempGameTitleList,
          })

        }
        // console.log("调用云函数获取报名记录成功", that.data.user)
      },
      fail: res => {
        console.log("调用云函数获取报名记录失败", res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})